package com.nova.scenic.projectlibs.util.graphics;

import android.graphics.*;

/**
 * Created by scenic on 2015/3/28.
 */
public class BitmapColorUtils {
    /**
     * @param bitmap
     * @param hue 色调
     * @param saturation 饱和度
     * @param lum
     * @return
     */
    public static Bitmap handlerImageEffect(Bitmap bitmap, float hue, float saturation, float lum) {

        Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bmp);

        Paint paint = new Paint();
        paint.setAntiAlias(true);


        ColorMatrix hueMatrix = new ColorMatrix();
        hueMatrix.setRotate(0, hue);
        hueMatrix.setRotate(1, hue);
        hueMatrix.setRotate(2, hue);

        ColorMatrix saturationMatrix = new ColorMatrix();
        saturationMatrix.setSaturation(saturation);

        ColorMatrix lumMatrix = new ColorMatrix();
        lumMatrix.setScale(lum, lum, lum, 1);

        ColorMatrix iamgeMatrix = new ColorMatrix();
        iamgeMatrix.postConcat(hueMatrix);
        iamgeMatrix.postConcat(saturationMatrix);
        iamgeMatrix.postConcat(lumMatrix);

        paint.setColorFilter(new ColorMatrixColorFilter(iamgeMatrix));

        canvas.drawBitmap(bitmap, 0, 0, paint);
        return bmp;
    }


    public static Bitmap handlerImageNegative(Bitmap bitmap){
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap bmp  = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        int [] oldPixels = new int[width * height];
        int [] newPixels = new int[oldPixels.length];
        bitmap.getPixels(oldPixels,0,width,0,0,width,height);

        int color;

        int r,g,b,a;
        for(int i = 0;i  < oldPixels.length;i++){
            color = oldPixels[i];
            r = Color.red(color);
            g = Color.green(color);
            b = Color.blue(color);
            a = Color.alpha(color);


            r = 255 - r;
            g = 255 - g;
            b = 255 - b;
            if(r > 255) {
                r = 255;
            }
            if(r < 0) {
                r = 0;
            }

            if(g > 255) {
                g = 255;
            }
            if(g < 0) {
                g = 0;
            }


            if(b > 255) {
                b = 255;
            }
            if(b < 0) {
                b = 0;
            }

            newPixels[i]= Color.argb(a,r,g,b);

        }

        bmp.setPixels(newPixels,0,width,0,0,width,height);
        return bmp;
    }

    public static Bitmap handlerImageOld(Bitmap bitmap){
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap bmp  = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        int [] oldPixels = new int[width * height];
        int [] newPixels = new int[oldPixels.length];
        bitmap.getPixels(oldPixels,0,width,0,0,width,height);



        int r,g,b,a;
        for(int i = 0;i  < oldPixels.length;i++){
            int color = oldPixels[i];
            r = Color.red(color);
            g = Color.green(color);
            b = Color.blue(color);
            a = Color.alpha(color);


            int r1 = (int) (0.393f * r + 0.769f * g + 0.189f * b);
            int g1 = (int) (0.349f * r + 0.686f * g + 0.168f * b);
            int b1 = (int) (0.272f * r + 0.534f * g + 0.131f * b);


            if(r1 > 255) {
                r1 = 255;
            }
            if(r1 < 0) {
                r1 = 0;
            }

            if(g1 > 255) {
                g1 = 255;
            }
            if(g1 < 0) {
                g1 = 0;
            }


            if(b1 > 255) {
                b1 = 255;
            }
            if(b1 < 0) {
                b1 = 0;
            }

            newPixels[i]= Color.argb(a,r1,g1,b1);

        }

        bmp.setPixels(newPixels,0,width,0,0,width,height);
        return bmp;
    }
}
